package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallMechanismMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LitemallMechanismService {
    @Resource
    private LitemallMechanismMapper mechanismMapper;
    @Resource
    private LitemallUserMapper litemallUserMapper;
    public List<LitemallMechanism> queryIndex() {
        LitemallMechanismExample example = new LitemallMechanismExample();
         return mechanismMapper.selectByExample(example);
    }

    public List<LitemallMechanism> queryEndUser(LocalDateTime time){
        LitemallMechanismExample example = new LitemallMechanismExample();
        example.or().andEndTimeLessThan(time).andLogicalDeleted(false);
        return mechanismMapper.selectByExample(example);
    }

    public LitemallMechanism queryByMobile(String mobile){
        LitemallMechanismExample example = new LitemallMechanismExample();
        example.or().andTelephoneEqualTo(mobile).andLogicalDeleted(false);
        return mechanismMapper.selectOneByExample(example);
    }

    public List<LitemallMechanism> querySelective(String name, String nickName, String legalPerson, String telephone, Integer page, Integer limit, String sort,String mcategoryId,String level,List<Byte> listB,String order) {
        LitemallMechanismExample example = new LitemallMechanismExample();
        LitemallMechanismExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(nickName)){
            criteria.andNickNameLike("%" + nickName + "%");
        }
        if(!StringUtils.isEmpty(legalPerson)){
            criteria.andLegalPersonLike("%" + legalPerson + "%");
        }
        if(!StringUtils.isEmpty(telephone)){
            criteria.andTelephoneEqualTo(telephone);
        }
        if(!StringUtils.isEmpty(mcategoryId)){
            criteria.andMcategoryIdEqualTo(Integer.parseInt(mcategoryId));
        }
        if(!StringUtils.isEmpty(level)){
            criteria.andLevelEqualTo((byte)Integer.parseInt(level));
        }
        criteria.andDeletedEqualTo(false).andCheckStatusIn(listB);

        PageHelper.startPage(page, limit);
        return mechanismMapper.selectByExample(example);
    }

    public List<LitemallMechanism> queryLikeName(String level,String id,String name){
        System.out.println("level=="+level+"id=="+id+"name=="+name);
        LitemallMechanismExample example = new LitemallMechanismExample();
        example.or().andNameLike(name+"%").andIdNotEqualTo(Integer.parseInt(id)).andLevelGreaterThanOrEqualTo((byte)Integer.parseInt(level));
        return mechanismMapper.selectByExample(example);
    }

    public List<LitemallMechanism> queryCheckMeg(String telephone){
        LitemallMechanismExample example = new LitemallMechanismExample();
        example.or().andTelephoneEqualTo(telephone).andDeletedEqualTo(false).andCheckStatusNotEqualTo((byte)0);
        return  mechanismMapper.selectByExample(example);
    }

    public List<LitemallMechanismVo2> queryCheckMechanism(Integer page, Integer limit,Byte value,String name, String sort, String order){
        Integer currIndex = (page-1)*10;
        return mechanismMapper.queryNewProxyMechanism(value,name,currIndex,limit);
    }

    public int countCheckMechanism(Integer page, Integer limit,Byte value,String name, String sort, String order){

        return mechanismMapper.countNewProxyMechanism(value,name);
    }

    public List<LitemallMechanism> queryTotalSelective(String name, String nickName, String legalPerson, String telephone, String sort, String order) {
        LitemallMechanismExample example = new LitemallMechanismExample();
        LitemallMechanismExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(nickName)){
            criteria.andNickNameLike("%" + nickName + "%");
        }
        if(!StringUtils.isEmpty(legalPerson)){
            criteria.andLegalPersonLike("%" + legalPerson + "%");
        }
        if(!StringUtils.isEmpty(telephone)){
            criteria.andTelephoneEqualTo(telephone);
        }
        criteria.andDeletedEqualTo(false);

        return mechanismMapper.selectByExample(example);
    }

    public int countSelective(String name, String nickName, String legalPerson, String telephone, Integer page, Integer size, String sort,String mcategoryId,String level,List<Byte> listB, String order) {
        LitemallMechanismExample example = new LitemallMechanismExample();
        LitemallMechanismExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(nickName)){
            criteria.andNickNameLike("%" + nickName + "%");
        }
        if(!StringUtils.isEmpty(legalPerson)){
            criteria.andLegalPersonLike("%" + legalPerson + "%");
        }
        if(!StringUtils.isEmpty(telephone)){
            criteria.andTelephoneEqualTo(telephone);
        }
        if(!StringUtils.isEmpty(mcategoryId)){
            criteria.andMcategoryIdEqualTo(Integer.parseInt(mcategoryId));
        }
        if(!StringUtils.isEmpty(level)){
            criteria.andLevelEqualTo((byte)Integer.parseInt(level));
        }
        criteria.andDeletedEqualTo(false).andCheckStatusIn(listB);

        return (int)mechanismMapper.countByExample(example);
    }

    public int countSelective(String status, Integer id, String name) {
        LitemallMechanismExample example = new LitemallMechanismExample();
        LitemallMechanismExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameEqualTo(name);
        }
        if (status != null && !StringUtils.isEmpty(status) && status.equals("update")) {
            criteria.andIdNotEqualTo(id);
        }
        criteria.andDeletedEqualTo(false);
        return (int)mechanismMapper.countByExample(example);
    }

    public int updateById(LitemallMechanism ad) {
        return mechanismMapper.updateByPrimaryKeySelective(ad);
    }

    public void updateAll(LitemallMechanism litemallMechanism){
        mechanismMapper.updateByPrimaryKey(litemallMechanism);
    }

    public void deleteById(Integer id) {
        mechanismMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallMechanism ad) {
        mechanismMapper.insertSelective(ad);
    }

    public LitemallMechanism findById(Integer id) {
        return mechanismMapper.selectByPrimaryKey(id);
    }

    public LitemallMechanism findValid(Integer id){
        LitemallMechanismExample example = new LitemallMechanismExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return mechanismMapper.selectOneByExample(example);
    }

    public LitemallMechanism findByReferCode(String id) {
        LitemallMechanismExample example = new LitemallMechanismExample();
        example.or().andReferCodeEqualTo(id);
        return mechanismMapper.selectOneByExample(example);
    }

    public int countMechanism(String name, String legalPerson){
        return (int)mechanismMapper.queryMechanismCount(name,legalPerson);
    }

    public int countLegalPerson(String legalPerson){
        return (int)mechanismMapper.queryLegalPerson(legalPerson);
    }
    public LitemallUser selectUser(String mobile){
        LitemallUserExample example = new LitemallUserExample();
        example.or().andMobileEqualTo(mobile);
        return litemallUserMapper.selectOneByExample(example);
    }
    public int countName(String name){
        return (int)mechanismMapper.queryName(name);
    }

    public List<LitemallMechanismVo> queryByLocate(String category,String longitude,String latitude){

        return mechanismMapper.queryMechanism(category, longitude, latitude);
    }

    public List<LitemallMechanismVo> searchMechanism(String longitude, String latitude, String name){

        return mechanismMapper.showMechanism(longitude, latitude, name);
    }

    public  List<LitemallMechanismVo> searchAllMechanism(String longitude, String latitude, String name, Integer page,Integer size){
       Integer currIndex = (page-1)*size;
       return mechanismMapper.queryAllMechanism(longitude, latitude, name,currIndex,size);
    }
}
